//
//  XJ_ApplicationViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/7.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_ApplicationViewController.h"
#import "XJ_ApplicationView.h"
#import "XJ_AppListModel.h"
#import "XJ_ApplicationCollectionViewCell.h"
#import "XJ_ApplicationDetailViewController.h"
#import "XJ_ApplicationHazyView.h"
@interface XJ_ApplicationViewController ()<UIGestureRecognizerDelegate,UIScrollViewDelegate>

@property (nonatomic, strong)NSMutableArray * appListArray;
@property (nonatomic, strong)UICollectionView  *collectionView;
@property (nonatomic, strong)UIScrollView * backScroll;
@property (nonatomic, strong)UIControl * control;
@end

@implementation XJ_ApplicationViewController

- (UIControl *)control{
    
    if (_control == nil) {
        
        _control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _control.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        [_control addTarget:self action:@selector(canceHazyViewAction) forControlEvents:UIControlEventTouchUpInside];
        
        XJ_ApplicationHazyView *hazyView =[[XJ_ApplicationHazyView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, 190)];
        
        hazyView.layer.position =self.view.center;
        
        [hazyView.sureButton addTarget:self action:@selector(canceHazyViewAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_control addSubview:hazyView];
        
    }
    
    return _control;
}

- (void)canceHazyViewAction{
    
    [self.control removeFromSuperview];
}

- (UIScrollView *)backScroll
{
    if (_backScroll == nil) {
        
        _backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT- SafeAreaTopHeight - SafeAreaBottomHeight )];
        _backScroll.showsVerticalScrollIndicator = NO;
        _backScroll.delegate = self;
//        _backScroll.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
    }
    
    return _backScroll;
}

- (NSMutableArray *)appListArray{
    
    if (_appListArray == nil) {
        
        _appListArray = [NSMutableArray array];
        
    }
    
    return _appListArray;
}

- (void)viewWillAppear:(BOOL)animated{
    
     [self loadApplicationList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =WhiteColor;
    self.nav_titleLabel.text =@"应用";
    self.nav_backButton.hidden =YES;
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:self.backScroll];
   
    

}

-(void)creatMainUI{
    
    float w = (SCREEN_WIDTH - 30 )/3;
    float h = 100;
    
    for (int i = 0;i<self.appListArray.count; i++) {
        
        int hang = i/3;//所在行
        int yushu = i%3;
        XJ_AppListModel * appModel = [self.appListArray objectAtIndex:i];
        XJ_ApplicationView * appIconView = [[XJ_ApplicationView alloc]initWithFrame:CGRectMake(15 + w*yushu, 15 + hang * h, w, h)];
        [appIconView.iconImage sd_setImageWithURL:[NSURL URLWithString:appModel.logoUrl]];
        appIconView.titleLabel.text = appModel.appName;
        appIconView.tag = 1000+i;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushAppDetail:)];
        tap.delegate = self;
        UIView *tapView = [tap view];
        tapView.tag = 1000 + i;
        [appIconView addGestureRecognizer:tap];
        
        
       
        
        //是第一行， 左边显示三条线 中间显示四条线，右边显示三条线； 其他行，左边显示两条线，中间显示三条线，右边显示两条线
        if (hang == 0) {
            
            if (yushu == 0) {
                
                appIconView.topLabel.hidden = NO;
                appIconView.bottomLabel.hidden = NO;
                appIconView.leftLabel.hidden = NO;
                appIconView.rightLabel.hidden = YES;
                
            }else if (yushu == 1){
                
                appIconView.topLabel.hidden = NO;
                appIconView.bottomLabel.hidden = NO;
                appIconView.leftLabel.hidden = NO;
                appIconView.rightLabel.hidden = NO;
                
            }else if (yushu == 2){
                
                appIconView.topLabel.hidden = NO;
                appIconView.bottomLabel.hidden = NO;
                appIconView.leftLabel.hidden = YES;
                appIconView.rightLabel.hidden = NO;
            }
            
            
        }else{
            
            if (yushu == 0) {
                
                appIconView.topLabel.hidden = YES;
                appIconView.bottomLabel.hidden = NO;
                appIconView.leftLabel.hidden = NO;
                appIconView.rightLabel.hidden = YES;
                
            }else if (yushu == 1){
                
                appIconView.topLabel.hidden = YES;
                appIconView.bottomLabel.hidden = NO;
                appIconView.leftLabel.hidden = NO;
                appIconView.rightLabel.hidden = NO;
                
            }else if (yushu == 2){
                
                appIconView.topLabel.hidden = YES;
                appIconView.bottomLabel.hidden = NO;
                appIconView.leftLabel.hidden = YES;
                appIconView.rightLabel.hidden = NO;
            }
        }
       
        //如果最后一个截止在最左侧，那么最后一个显示三条线；如果此时只有一行，那么最左侧就显示四条线
        if (i == self.appListArray.count - 1 ) {
            
            if (i%3 == 0) {
                
                if (hang == 0) {
                    
                    appIconView.topLabel.hidden = NO;
                    appIconView.bottomLabel.hidden = NO;
                    appIconView.leftLabel.hidden = NO;
                    appIconView.rightLabel.hidden = NO;
                    
                }else{
                    
                    appIconView.topLabel.hidden = YES;
                    appIconView.bottomLabel.hidden = NO;
                    appIconView.leftLabel.hidden = NO;
                    appIconView.rightLabel.hidden = NO;
                }
                
                
            }
            
        }
        
        [self.backScroll addSubview:appIconView];
    }
    
    int listHang = self.appListArray.count % 3 == 0 ? (int)self.appListArray.count/3:(int)self.appListArray.count/3+1;
    
    self.backScroll.contentSize = CGSizeMake(SCREEN_WIDTH, listHang * 110 + 15);
}

- (void)pushAppDetail:(UIGestureRecognizer *)singleTap{
    
    NSInteger index = singleTap.view.tag - 1000;
    
    XJ_AppListModel * model = [self.appListArray objectAtIndex:index];
    
    if (model.link.length > 0 && model.link != nil) {
        
        XJ_ApplicationDetailViewController * detailVC = [[XJ_ApplicationDetailViewController alloc]init];
        detailVC.urlString = model.link;
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }else{
        
        [KWINDOW addSubview:self.control];
    }
   
    NSLog(@"index = %ld",(long)index);
}
#pragma mark--获取应用列表
-(void)loadApplicationList{
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [XJ_Utils signPost:@"application/list?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        [self.appListArray removeAllObjects];
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        NSArray * appListArray = [dataDic objectForKey:@"appList"];
        
        if (appListArray.count > 0) {
            
            for (NSDictionary * dic in appListArray) {
                
                XJ_AppListModel * model = [XJ_AppListModel mj_objectWithKeyValues:dic];
                
                [self.appListArray addObject:model];
            }
        }
        
       [self creatMainUI];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
